package com.rongzhe.house.presenter;

import android.text.TextUtils;

import com.rongzhe.house.entity.HouseEntrustTo;
import com.rongzhe.house.internet.DataListener;
import com.rongzhe.house.manager.HouseManager;
import com.rongzhe.house.ui.UiControlInterface;
import com.rongzhe.house.utils.StringUtils;

/**
 * Created by huang on 2017/12/13.
 */

public class HouseEntrustPresenter extends BasePresenter {
    public HouseEntrustPresenter(UiControlInterface uiControlInterface) {
        super(uiControlInterface);
    }

    private boolean checkForData(HouseEntrustTo houseEntrust) {
        if (houseEntrust == null) {
            getUiControlInterface().showToast("数据检查失败!");
            return false;
        }
        if (!StringUtils.isChinaPhoneLegal(houseEntrust.getMobile())) {
            getUiControlInterface().showToast("请输入正确的手机号码!");
            return false;
        }
        if (!checkData(houseEntrust.getRealName())) {
            getUiControlInterface().showToast("请填入正确的真实姓名!");
            return false;
        }
        if (!checkData(houseEntrust.getNeighborhoodName())) {
            getUiControlInterface().showToast("请填入正确的小区名称!");
            return false;
        }
        return true;
    }

    private boolean checkData(String str) {
        return !TextUtils.isEmpty(str) && str.length() >= 2;
    }

    public void doEntrust(HouseEntrustTo houseEntrust) {
        if(!checkForData(houseEntrust)) {
            return;
        }
        HouseManager.getInstance().houseEntrust(houseEntrust, new DataListener<Object>() {
            @Override
            public void onSuccess(Object data, String message) {
                getUiControlInterface().showToast("委托成功, 我们将很快联系您!");
                getUiControlInterface().finish();
            }

            @Override
            public void onFailed(Throwable error, String message) {
                getUiControlInterface().showToast("委托失败, 请稍后再试!");
            }

            @Override
            public void onEnd() {

            }
        });
    }
}
